<?php

namespace App\ApiFrontend\User;

use App\Http\Controllers\ApiBaseController;
use App\Models\UserModel;
use Illuminate\Support\Facades\Hash;


/**
 *  添加或编辑
 */
class Add extends ApiBaseController
{

  /**
   * 参数检查
   */
  protected function check(): void
  {
    try {
      request()->validate([
        'username' => 'required|string',
        'phone' => 'required',
      ]);
    } catch (\Exception $exception) {
      $this->result["code"] = 1;
      $this->result["message"] = "参数错误: " . $exception->getMessage();
    }
  }

  /**
   * 业务主体
   */
  protected function service(): void
  {
    //数据处理
    try {
      $userModel = new UserModel();
      $userModel->username = request('username');
      $userModel->phone = request('phone');
      $userModel->status = 1;
      $userModel->password = Hash::make(\request('password') ?? '123456');
      $userModel->save();
    } catch (\Exception $exception) {
      $this->result["code"] = 1;
      $this->result["message"] = $exception->getMessage();
    }
  }
}
